package com.wqr.controller;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.wqr.pojo.Delicious;
import com.wqr.pojo.DeliciousUser;
import com.wqr.pojo.Login;
import com.wqr.pojo.User;
import com.wqr.service.DeliciousService;
import com.wqr.service.DeliciousUserService;
import com.wqr.service.UserService;

@Controller
public class DeliciousController extends BaseController {
	@Autowired
    public DeliciousService deliciousService;
	@Autowired
	public DeliciousUserService deliciousUserService;
	@Autowired
	public UserService userService;
	
	
	@RequestMapping("/addDelicious")
	//MultipartFile spring接口，接收form表单提交的文件，然后转化成文件
	public String addDelicious(MultipartFile txtDeliciousPicture,Delicious delicious, Login login,Model model,
			String txtDeliciousName,String txtDeliciousPractice,Integer txtChannelId,DeliciousUser deliciousUser,String txtRemark,
			HttpSession session) throws Exception {
		login = (Login)session.getAttribute("login");
		//1. 获取图片完整名称
		String fileStr = txtDeliciousPicture.getOriginalFilename();
		//2. 使用随机生成的字符串+源图片扩展名组成新的图片名称,防止图片重名
		String newfileName = UUID.randomUUID().toString() + fileStr.substring(fileStr.lastIndexOf("."));
		delicious.setDeliciousName(txtDeliciousName);
		delicious.setDeliciousPractice(txtDeliciousPractice);
		//4.将图片名称保存到数据库
		delicious.setDeliciousPicture(newfileName);
		
		User list = userService.selectUserByLoginId(login.getLoginId());
		if(list != null) {
			delicious.setUserId(list.getUserId());
			delicious.setChannelId(txtChannelId);
			delicious.setDeliciousTime(new Date());
			int a = deliciousService.insertDelicious(delicious);
			if(a != 0){
				//3. 将图片保存到硬盘
				txtDeliciousPicture.transferTo(new File("C:\\img\\" + newfileName));
			}
			//添加deliciousUser数据
			int b = deliciousService.selectDeliciousId();
			deliciousUser.setUserId(delicious.getUserId());
			deliciousUser.setDeliciousId(b);
			deliciousUser.setDescription(delicious.getDeliciousName());
			deliciousUser.setRemark("所属");
			deliciousUserService.insertDeliciousUser(deliciousUser);
			return "redirect:manageDelicious.action";
		}else {
			model.addAttribute("alertMsg", getAlertScript("请先完善用户信息！！！"));
			return "manageDelicious";
		}
	}
	//JSP请求路径，请求时需加后缀.action
	@RequestMapping("/userAddDelicious")
	//Controller方法接收的参数的变量名称必须要等于页面上input框的name属性值
	//MultipartFile spring接口，接收form表单提交的文件，然后转化成文件
	public String userAddDelicious(MultipartFile txtDeliciousPicture,Delicious delicious, Login login,Model model,
			String txtDeliciousName,String txtDeliciousPractice,Integer txtChannelId,DeliciousUser deliciousUser,String txtRemark,
			HttpSession session) throws Exception {
		login = (Login)session.getAttribute("login");
		//获取图片完整名称
		String fileStr = txtDeliciousPicture.getOriginalFilename();
		//使用随机生成的字符串+源图片扩展名组成新的图片名称,防止图片重名
		String newfileName = UUID.randomUUID().toString() + fileStr.substring(fileStr.lastIndexOf("."));
		//将图片名称添加到delicious
		delicious.setDeliciousPicture(newfileName);
		//向delicious添加数据
		delicious.setDeliciousName(txtDeliciousName);
		delicious.setDeliciousPractice(txtDeliciousPractice);
		//将图片名称保存到数据库
		delicious.setDeliciousPicture(newfileName);
		
		User list = userService.selectUserByLoginId(login.getLoginId());
		if(list != null) {
			delicious.setUserId(list.getUserId());
			delicious.setChannelId(txtChannelId);
			delicious.setDeliciousTime(new Date());
			//执行添加操作
			int a = deliciousService.insertDelicious(delicious);
			//添加成功后执行
			if(a != 0){
				//将图片保存到硬盘
				txtDeliciousPicture.transferTo(new File("C:\\img\\" + newfileName));
			}
			//添加deliciousUser数据
			deliciousUser.setUserId(delicious.getUserId());
			deliciousUser.setDeliciousId(deliciousService.selectDeliciousId());
			deliciousUser.setDescription(delicious.getDeliciousName());
			deliciousUser.setRemark("所属");
			//执行添加操作
			deliciousUserService.insertDeliciousUser(deliciousUser);
			//以redirect:字符串开头的都为重定向，返回页面
			return "redirect:userDelicious.action";
		}else {
			model.addAttribute("alertMsg", getAlertScript("请先完善用户信息！！！"));
			return "userAddDelicious";
		}
	}
	//@RequestMapping(value="/deleteDelicious/{userStateId}",method=RequestMethod.GET)
	@RequestMapping("/deleteDelicious")
	@ResponseBody// 返回的数据不是html标签的页面，而是其他某种格式的数据时（如json、xml等）使用；
	public int deleteDelicious(@RequestBody //读取Request请求的body部分数据,再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上
			Delicious delicious) throws Exception {
		//@pathVariable //将 URL 中占位符参数绑定到控制器处理方法的入参中 URL 中的 {xxx} 占位符可以通过@PathVariable("xxx") 绑定到操作方法的入参中。
		int a = deliciousService.delectDelicious(delicious.getDeliciousId());
		return a;
	}
	//JSP请求路径，请求时需加后缀.action
	@RequestMapping("/updateDelicious")//修改时查询要修改菜谱的信息
	@ResponseBody//返回的数据不是html标签的页面,是其他某种格式的数据时（如json、xml等）使用
	//@RequestBody 读取Request请求的body部分数据,再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上
	public List<Delicious> updateDelicious(@RequestBody Delicious delicious,Model model) throws Exception {
		//执行查询操作
		List<Delicious> itemList = deliciousService.list(delicious);
		//将接收到的数据返回JSP界面
		model.addAttribute("deliciousModify", delicious);
		//返回查询结果
		return itemList;
	}
	//JSP请求路径，请求时需加后缀.action
	@RequestMapping("/saveDelicious")
	@ResponseBody// 返回的数据不是html标签的页面，而是其他某种格式的数据时（如json、xml等）使用；
	public int saveDelicious(@RequestBody Delicious delicious) throws Exception {
		int a = deliciousService.updateDelicious(delicious);
		return a;
	}
	@RequestMapping("/searchDelicious")
	@ResponseBody
	public List<Delicious> searchDelicious(@RequestBody Delicious delicious) throws Exception {
		if(delicious.getDeliciousName() == ""){
			delicious.setDeliciousName(null);
		}
		List<Delicious> itemList = deliciousService.selectDelicious(delicious);
	    return itemList;
	}
	//JSP请求路径，请求时需加后缀.action
	@RequestMapping("/userSearchDelicious")
	//Controller方法接收的参数的变量名称必须要等于页面上input框的name属性值
	public String userSearchDelicious(String Search,Integer agileinfo_search,Delicious delicious,Model model) throws Exception {
		//处理查询信息
		if(Search != "" && Search != null){
			delicious.setDeliciousName(Search);
		}else {
			delicious.setDeliciousName(null);
		}
		delicious.setChannelId(agileinfo_search);
		//执行查询操作
		List<Delicious> itemList = deliciousService.selectDelicious(delicious);
		//判断是否为空，为空则提示
		if(itemList.isEmpty()) {
			model.addAttribute("alertMsg", getAlertScript("未找到结果！"));
		}
		//Model模型:模型中放入了返回给页面的数据，底层其实就是用的request域来传递数据,但是对request域进行了扩展.
		model.addAttribute("itemList", itemList);
		//如果springMvc方法返回一个简单的string字符串,那么springMvc就会认为这个字符串就是页面的名称
	    return "userDelicious"; 
	}
	
	@RequestMapping("/manageDelicious")
	public String manageDelicious(Model model,Delicious delicious) throws Exception {
		List<Delicious> list = deliciousService.list(delicious);
		model.addAttribute("itemList", list);
	    return "manageDelicious"; 
	}
	@RequestMapping("/userAllDelicious")
	@ResponseBody
	public List<Delicious> userAllDelicious(@RequestBody Delicious delicious) throws Exception {
		List<Delicious> list = deliciousService.list(delicious);
	    return list; 
	}
	@RequestMapping("/allDelicious")
	@ResponseBody
	public List<Delicious> allDelicious() throws Exception {
		List<Delicious> list = deliciousService.list();
	    return list; 
	}
	@RequestMapping("/userDelicious")
	public String userDelicious(Model model,Delicious delicious) throws Exception {
		List<Delicious> list = deliciousService.list(delicious);
		model.addAttribute("itemList", list);
	    return "userDelicious"; 
	}
	@RequestMapping("/userModifyDelicious")
	public String userModifyDelicious(@RequestParam("deliciousId") Integer deliciousId,Model model) throws Exception {
		model.addAttribute("deliciousId", deliciousId);
	    return "userModifyDelicious"; 
	}
}
